<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button
          type="primary"
          @click="handlePrint({})"
          v-auth="'bas:mes:operator:basOperator:view'"
        >
          <Icon icon="ant-design:printer-outlined" /> {{ t('工牌打印') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBasMesOperatorBasOperatorList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    basOperatorDelete,
    basOperatorListData,
    basOperatorDisable,
    basOperatorEnable,
  } from '/@/api/bas/mes/operator/basOperator';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { useModal } from '/@/components/Modal';
  import { basWorkShopTreeData } from '/@/api/bas/mes/workshop/basMesWorkShop';
  import PrintModal from '/@/components/print/printModal.vue';

  const { t } = useI18n('bas.mes.operator.basOperator');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('操作工档案管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('操作工编码'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('操作工名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('归属车间'),
        field: 'workShopCode',
        component: 'TreeSelect',
        componentProps: {
          api: basWorkShopTreeData,
          params: { isLoadUser: true, userIdPrefix: '' },
          canSelectParent: false,
          allowClear: true,
        },
      },
      {
        label: t('是否用户'),
        field: 'buser',
        component: 'CheckboxGroup',
        componentProps: {
          dictType: 'sys_yes_no',
        },
      },
      {
        label: t('用户名称'),
        field: 'user.userName',
        component: 'Input',
      },
      {
        label: t('入职日期'),
        field: 'inDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('联系电话'),
        field: 'iphone',
        component: 'Input',
      },
      {
        label: t('离职日期'),
        field: 'overDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
      },
      {
        label: t('证件号码'),
        field: 'carno',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'sys_search_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('操作工编码'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('操作工名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('归属车间'),
      //dataIndex: 'workShopCode',
      dataIndex: 'workShop.name',
      key: 'shop.name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('是否用户'),
      dataIndex: 'buser',
      key: 'a.buser',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_yes_no',
    },
    {
      title: t('用户名称'),
      dataIndex: 'user.userName',
      key: 'a.user_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('入职日期'),
      dataIndex: 'inDate',
      key: 'a.in_date',
      sorter: true,
      width: 150,
      align: 'center',
    },
    {
      title: t('联系电话'),
      dataIndex: 'iphone',
      key: 'a.iphone',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('离职日期'),
      dataIndex: 'overDate',
      key: 'a.over_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('证件号码'),
      dataIndex: 'carno',
      key: 'a.carno',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 130,
      align: 'center',
      dictType: 'sys_search_status',
    },
    {
      title: t('更新日期'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('备注说明'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 130,
      align: 'left',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑操作工档案'),
        onClick: handleForm.bind(this, { code: record.code }),
        auth: 'bas:mes:operator:basOperator:edit',
      },
      {
        icon: 'ant-design:stop-outlined',
        color: 'error',
        title: t('停用操作工档案'),
        popConfirm: {
          title: t('是否确认停用操作工档案'),
          confirm: handleDisable.bind(this, { code: record.code }),
        },
        auth: 'bas:mes:operator:basOperator:edit',
        ifShow: () => record.status === '0',
      },
      {
        icon: 'ant-design:check-circle-outlined',
        color: 'success',
        title: t('启用操作工档案'),
        popConfirm: {
          title: t('是否确认启用操作工档案'),
          confirm: handleEnable.bind(this, { code: record.code }),
        },
        auth: 'bas:mes:operator:basOperator:edit',
        ifShow: () => record.status === '2',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除操作工档案'),
        popConfirm: {
          title: t('是否确认删除操作工档案'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        auth: 'bas:mes:operator:basOperator:delete',
      },
    ],
  };

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: basOperatorListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/bas/mes/operator/basOperator/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDisable(record: Recordable) {
    const res = await basOperatorDisable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleEnable(record: Recordable) {
    const res = await basOperatorEnable(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleDelete(record: Recordable) {
    const res = await basOperatorDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 用户工牌打印
  async function handlePrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const idsArr = arr.map((item) => {
      return item.code;
    });
    let params = {
      ids: idsArr.join(','),
      title: '用户工牌打印',
      height: '0.9',
      width: '0.9',
      fileName: '用户工牌',
    };
    openPrintModal(true, params);
  }

  function handleSuccess() {
    reload();
  }
</script>
